package com.ivt.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ivt.core.ex.IvtException;
import com.ivt.dao.ClassesDao;
import com.ivt.dao.SpecificDao;
import com.ivt.model.Classes;
import com.ivt.model.view.QueryCriteria;
import com.ivt.service.ClassesService;
import com.ivt.utils.enums.AuditStatus;

@Service
public class ClssesServiceImpl implements ClassesService {

	@Resource
	private ClassesDao classesDao;

	@Resource
	private SpecificDao specificDao;

	@Override
	public Classes create(Classes classes) throws IvtException {

		if (null == classes) {
			throw new IvtException("班级信息不能为空");
		}
		if (StringUtils.isBlank(classes.getName())) {
			throw new IvtException("班级名称不能为空");
		}
		if (StringUtils.isBlank(classes.getDescription())) {
			throw new IvtException("班级描述不能为空");
		}

		long c = classesDao.existsByName(classes.getName());
		if (0 < c) {
			throw new IvtException("班级【" + classes.getName() + "】已存在");
		}

		classes.setClassesId(null);
		classes.setStatus(AuditStatus.PASS);
		return classesDao.saveAndFlush(classes);
	}

	@Override
	public Classes update(Classes classes) throws IvtException {
		if (null == classes || null == classes.getClassesId()
				|| 0 >= classes.getClassesId()) {
			throw new IvtException("班级信息不能为空");
		}
		if (StringUtils.isBlank(classes.getName())
				|| StringUtils.isBlank(classes.getDescription())) {
			throw new IvtException("参数错误,没有要修改的项目");
		}

		if (StringUtils.isNotBlank(classes.getName())
				&& 0 < classesDao.existsByNameAndId(classes.getName(),
						classes.getClassesId())) {
			throw new IvtException("班级【" + classes.getName() + "】已存在");
		}
		classes.setStatus(AuditStatus.PASS);
		return classesDao.saveAndFlush(classes);

	}

	@Override
	public Classes delete(Long classesId) throws IvtException {
		if (null == classesId || 0 >= classesId) {
			throw new IvtException("参数错误");
		}

		Classes classes = new Classes();
		classes.setClassesId(classesId);
		classes.setStatus(AuditStatus.NOT);
		return classesDao.saveAndFlush(classes);
	}

	@Override
	public Classes getClasses(Long classesId) throws IvtException {
		if (null == classesId || 0 >= classesId) {
			throw new IvtException("参数错误");
		}
		return classesDao.findOne(classesId);
	}

	@Override
	public List<Classes> getClasses(Set<Long> classesId) throws IvtException {
		if (CollectionUtils.isEmpty(classesId)) {
			throw new IvtException("参数错误");
		}

		Iterable<Classes> coll = classesDao.findAll(classesId);
		return (null == coll) ? null : Lists.newArrayList(coll);
	}

	@Override
	public List<Classes> queryClassesByCriteria(QueryCriteria queryCriteria)
			throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		return specificDao.queryClasses(queryCriteria);
	}

	@Override
	public long queryCount(QueryCriteria queryCriteria) throws IvtException {
		queryCriteria = cleanQueryCriteria(queryCriteria);
		
		return specificDao.queryClassesCount(queryCriteria);
	}

	@Override
	public int deletes(Long[] classesIds) throws IvtException {
		if (ArrayUtils.isEmpty(classesIds)) {
			throw new IvtException("参数错误");
		}

		Set<Long> cleanIds = Sets.newHashSet(classesIds);
		Iterable<Classes> classes = null;
		classes = classesDao.findAll(cleanIds);
		int i = 0;
		if (null != classes) {
			for (Classes cz : classes) {
				cz.setStatus(AuditStatus.NOT);
				classesDao.saveAndFlush(cz);
				i++;
			}
		}
		return i;
	}

	@Override
	public List<Classes> queryClassesByStatus(AuditStatus status)
			throws IvtException {
		if (null == status) {
			return null;
		}
		return classesDao.findByStatus(status);
	}

	protected QueryCriteria cleanQueryCriteria(QueryCriteria queryCriteria) {

		if (null == queryCriteria) {
			return null;
		}
		Map<String, Object> criteria = queryCriteria.splitCriteria();

		if (MapUtils.isNotEmpty(criteria)) {

			if (criteria.containsKey("status")
					&& StringUtils.isNumeric(String.valueOf(criteria
							.get("status")))) {
				AuditStatus status = AuditStatus.findByValue(Integer
						.valueOf(String.valueOf(criteria.get("status"))));
				criteria.put("status", status);
			}
		}

		return queryCriteria;
	}
}
